#apache日志分析
import re
def couint_patt(fname,patt):
    result = {}#用于保存结果的变量
    #编译正则表达式
    cpatt = re.compile(patt)
    with open(fname) as fobj:
        for line in fobj:
            m = cpatt.search(line)
            if m:
                key = m.group()
                result[key] = result.get(key,0) + 1
    return result
if __name__ == '__main__':
    logfile='access_log'
    ip = '^(\d+\.){3}\d+'
    br = 'Chrome|Firefox|MSIE'
    reslt1 = couint_patt(logfile,ip)
    reslt2 = couint_patt(logfile,br)
    print(reslt1)
    print(reslt2)